//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "LeastSquareProblem.h"
using namespace Cephei::QL::Math::Optimization;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Math/Matrix.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Math::Optimization::CLeastSquareProblem::CLeastSquareProblem (boost::shared_ptr<QuantLib::LeastSquareProblem>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLeastSquareProblem = NULL;
#endif
	_ppLeastSquareProblem = &childNative;
    
}
Cephei::QL::Math::Optimization::CLeastSquareProblem::CLeastSquareProblem (QuantLib::LeastSquareProblem& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phLeastSquareProblem = NULL;
#endif
	_ppLeastSquareProblem = new boost::shared_ptr<QuantLib::LeastSquareProblem> (&childNative);
    
    _LeastSquareProblemOwner = owner;
    
}

Cephei::QL::Math::Optimization::CLeastSquareProblem::CLeastSquareProblem (CLeastSquareProblem^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLeastSquareProblem = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLeastSquareProblem = new boost::shared_ptr<QuantLib::LeastSquareProblem> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Optimization::CLeastSquareProblem::CLeastSquareProblem (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLeastSquareProblem = NULL;
#endif
	if (!t->IsSubclassOf(CLeastSquareProblem::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Optimization::CLeastSquareProblem::CLeastSquareProblem (QuantLib::Handle<QuantLib::LeastSquareProblem>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phLeastSquareProblem = &childNative;
	_ppLeastSquareProblem = &static_cast<boost::shared_ptr<QuantLib::LeastSquareProblem>>(childNative.currentLink());
    
    _LeastSquareProblemOwner = owner;
}
Cephei::QL::Math::Optimization::CLeastSquareProblem::CLeastSquareProblem (QuantLib::Handle<QuantLib::LeastSquareProblem> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phLeastSquareProblem = &childNative;
	_ppLeastSquareProblem = &static_cast<boost::shared_ptr<QuantLib::LeastSquareProblem>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Optimization::CLeastSquareProblem::CLeastSquareProblem (QuantLib::LeastSquareProblem childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLeastSquareProblem = NULL;
#endif
	_ppLeastSquareProblem = new boost::shared_ptr<QuantLib::LeastSquareProblem> (new QuantLib::LeastSquareProblem (childNative));
    
}
#endif

Cephei::QL::Math::Optimization::CLeastSquareProblem::~CLeastSquareProblem ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppLeastSquareProblem != NULL)
    {
	    delete _ppLeastSquareProblem;
        _ppLeastSquareProblem = NULL;
    }
}
Cephei::QL::Math::Optimization::CLeastSquareProblem::!CLeastSquareProblem ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppLeastSquareProblem != NULL)
    {
	    delete _ppLeastSquareProblem;
    }
}
QuantLib::LeastSquareProblem& Cephei::QL::Math::Optimization::CLeastSquareProblem::GetReference ()
{
    if (_ppLeastSquareProblem == NULL) throw gcnew NativeNullException ();
	return **_ppLeastSquareProblem;
}
boost::shared_ptr<QuantLib::LeastSquareProblem>& Cephei::QL::Math::Optimization::CLeastSquareProblem::GetShared ()
{
    if (_ppLeastSquareProblem == NULL) throw gcnew NativeNullException ();
	return *_ppLeastSquareProblem;
}
QuantLib::LeastSquareProblem* Cephei::QL::Math::Optimization::CLeastSquareProblem::GetPointer ()
{
    if (_ppLeastSquareProblem == NULL) throw gcnew NativeNullException ();
	return &**_ppLeastSquareProblem;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LeastSquareProblem>& Cephei::QL::Math::Optimization::CLeastSquareProblem::GetHandle ()
{
	if (_phLeastSquareProblem == NULL)
	{
		_phLeastSquareProblem = new Handle<QuantLib::LeastSquareProblem> (*_ppLeastSquareProblem);
	}
	return *_phLeastSquareProblem;
}
#endif
bool Cephei::QL::Math::Optimization::CLeastSquareProblem::HasNative () 
{
	return (_ppLeastSquareProblem != NULL);
}

UInt64 Cephei::QL::Math::Optimization::CLeastSquareProblem::Size::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppLeastSquareProblem)->size ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Optimization::ILeastSquareProblem^ Cephei::QL::Math::Optimization::CLeastSquareProblem::TargetAndValue (Cephei::QL::Math::IArray^ x, Cephei::QL::Math::IArray^ target, Cephei::QL::Math::IArray^ fct2fit)
{
    CArray^ _Cx;
    CArray^ _Ctarget;
    CArray^ _Cfct2fit;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cx = safe_cast<CArray^> (x);
        _Cx->Lock();
        QuantLib::Array& _x = static_cast<QuantLib::Array&> (_Cx->GetReference ()); 
        _Ctarget = safe_cast<CArray^> (target);
        _Ctarget->Lock();
        QuantLib::Array& _target = static_cast<QuantLib::Array&> (_Ctarget->GetReference ()); 
        _Cfct2fit = safe_cast<CArray^> (fct2fit);
        _Cfct2fit->Lock();
        QuantLib::Array& _fct2fit = static_cast<QuantLib::Array&> (_Cfct2fit->GetReference ()); 
    	(*_ppLeastSquareProblem)->targetAndValue ( _x,  _target,  _fct2fit );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cx != nullptr) _Cx->Unlock();
        if (_Ctarget != nullptr) _Ctarget->Unlock();
        if (_Cfct2fit != nullptr) _Cfct2fit->Unlock();
    }
}
Cephei::QL::Math::Optimization::ILeastSquareProblem^ Cephei::QL::Math::Optimization::CLeastSquareProblem::TargetValueAndGradient (Cephei::QL::Math::IArray^ x, Cephei::QL::Math::IMatrix^ grad_fct2fit, Cephei::QL::Math::IArray^ target, Cephei::QL::Math::IArray^ fct2fit)
{
    CArray^ _Cx;
    CMatrix^ _Cgrad_fct2fit;
    CArray^ _Ctarget;
    CArray^ _Cfct2fit;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cx = safe_cast<CArray^> (x);
        _Cx->Lock();
        QuantLib::Array& _x = static_cast<QuantLib::Array&> (_Cx->GetReference ()); 
        _Cgrad_fct2fit = safe_cast<CMatrix^> (grad_fct2fit);
        _Cgrad_fct2fit->Lock();
        QuantLib::Matrix& _grad_fct2fit = static_cast<QuantLib::Matrix&> (_Cgrad_fct2fit->GetReference ()); 
        _Ctarget = safe_cast<CArray^> (target);
        _Ctarget->Lock();
        QuantLib::Array& _target = static_cast<QuantLib::Array&> (_Ctarget->GetReference ()); 
        _Cfct2fit = safe_cast<CArray^> (fct2fit);
        _Cfct2fit->Lock();
        QuantLib::Array& _fct2fit = static_cast<QuantLib::Array&> (_Cfct2fit->GetReference ()); 
    	(*_ppLeastSquareProblem)->targetValueAndGradient ( _x,  _grad_fct2fit,  _target,  _fct2fit );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cx != nullptr) _Cx->Unlock();
        if (_Cgrad_fct2fit != nullptr) _Cgrad_fct2fit->Unlock();
        if (_Ctarget != nullptr) _Ctarget->Unlock();
        if (_Cfct2fit != nullptr) _Cfct2fit->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

